package com.wxn.dao;
/*
 * Created by wxn
 * 2018/3/18 1:21
 */


import com.wxn.bean.Message;
import com.wxn.db.DBAccess;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 和message表相关的数据库操作
 */
public class MessageDao {
	/**
	 * 根据条件查询消息列表
	 */
	public List<Message> queryMessageList(Map<String,Object> parameter) {
		DBAccess dbAccess = new DBAccess();
		List<Message> messageList = new ArrayList<Message>();
		SqlSession sqlSession = null;
		try {
			sqlSession = dbAccess.getSqlSession();
			//通过sqlSession执行SQL语句
			IMessage iMessage = sqlSession.getMapper(IMessage.class);
			messageList = iMessage.queryMessageList(parameter);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
		return messageList;
	}
	/**
	 * 根据条件查询消息列表条数
	 */
	public int count(Message message) {
		DBAccess dbAccess = new DBAccess();
		SqlSession sqlSession = null;
		int result = 0;
		try {
			sqlSession = dbAccess.getSqlSession();
			//通过sqlSession执行SQL语句
			IMessage iMessage = sqlSession.getMapper(IMessage.class);
			result = iMessage.count(message);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
		return result;
	}

	/**
	 * 根据ID删除一条信息
	 */
	public void deleteOne(Integer id) {
		DBAccess dbAccess = new DBAccess();
		SqlSession sqlSession = null;
		try {
			sqlSession = dbAccess.getSqlSession();
			//通过sqlSession执行SQL语句
			IMessage iMessage = sqlSession.getMapper(IMessage.class);
			iMessage.deleteOne(id);
			sqlSession.commit();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}

	}

	/**
	 * 批量删除
	 */
	public void deleteBatch(List<Integer> idList) {
		DBAccess dbAccess = new DBAccess();
		SqlSession sqlSession;
		try {
			sqlSession = dbAccess.getSqlSession();
			IMessage iMessage = sqlSession.getMapper(IMessage.class);
			iMessage.deleteBatch(idList);
			sqlSession.commit();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 增加一个
	 */
	public void insertOne(Message message) {
		DBAccess dbAccess = new DBAccess();
		SqlSession sqlSession;
		try {
			sqlSession = dbAccess.getSqlSession();
			IMessage iMessage = sqlSession.getMapper(IMessage.class);
			iMessage.insertOne(message);
			sqlSession.commit();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public List<Message> queryMessageListByPage(Map<String, Object> parameter) {
		DBAccess dbAccess = new DBAccess();
		List<Message> messageList = new ArrayList<Message>();
		SqlSession sqlSession = null;
		try {
			sqlSession = dbAccess.getSqlSession();
			// 通过sqlSession执行SQL语句
			IMessage imessage = sqlSession.getMapper(IMessage.class);
			messageList = imessage.queryMessageListByPage(parameter);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(sqlSession != null) {
				sqlSession.close();
			}
		}
		return messageList;
	}
}
